#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2016-09-11 17:17:20
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

import os
import MySQLdb
import conf
import hashlib
import random
import base64
import sys

reload(sys)
sys.setdefaultencoding('utf-8')


def db_object():
	''' 数据库连接对象 '''
	db_info = conf.db_info
	try:
		conn = MySQLdb.connect(**db_info)
		cur = conn.cursor()
		return conn, cur

	except Exception, e:
		raise e
	finally:
		pass


# 条件查询
def db_select_on(query):
	'''
	数据库条件查询
	'''
	try:
		cur = db_object()[1]
		cur.execute(query)
		row = cur.fetchone()
		return row
	except Exception, e:
		return "Mysql Error {arg0}:{arg1}".format(arg0=e.args[0], arg1=e.args[1])


# 模糊全部
def db_select_all(query):
	'''
	数据库模糊查询
	'''
	cur = db_object()[1]
	try:
		cur.execute(query)
		row = cur.fetchall()
		return row
	except Exception, e:
		return "Mysql Error {arg0}:{arg1}".format(arg0=e.args[0], arg1=e.args[1])


# 单条插入，成功返回1，失败返回None
def db_insert_on(query=None, args=None):
	conn, cur = db_object()
	try:
		if args:
			result = cur.execute(query, args)
			conn.commit()
		else:
			result = cur.execute(query)
			conn.commit()
		return result
	except MySQLdb.Error, e:
		return "Mysql Error {arg0}:{arg1}".format(arg0=e.args[0], arg1=e.args[1])

# 多条插入，成功返回count，失败返回None
def db_insert_many(query=None, args=None):
	conn, cur = db_object()
	try:
		if args:
			result = cur.executemany(query, args)
			conn.commit()
		else:
			result = cur.executemany(query)
			conn.commit()
		return result
	except MySQLdb.Error, e:
		return "Mysql Error {arg0}:{arg1}".format(arg0=e.args[0], arg1=e.args[1])


# 更新，成功返回1，更新未改动返回0；
def db_update_on(query=None, args=None):
	conn, cur = db_object()
	try:
		if args:
			result = cur.execute(query, args)
			conn.commit()
		else:
			result = cur.execute(query)
			conn.commit()
		return result
	except MySQLdb.Error, e:
		return "Mysql Error {arg0}:{arg1}".format(arg0=e.args[0], arg1=e.args[1])


# 删除，成功返回1，更新未改动返回0；
def db_delete_on(query, args=None):
	conn, cur = db_object()
	try:
		if args:
			result = cur.execute(query, args)
			conn.commit()
		else:
			result = cur.execute(query)
			conn.commit()
		return result
	except MySQLdb.Error, e:
		return "Mysql Error {arg0}:{arg1}".format(arg0=e.args[0], arg1=e.args[1])


def db_close():
	conn, cur = db_object()
	cur.close()
	conn.close()


# MD5加密
def md5(strs):
	# import hashlib
	m = hashlib.md5()  # 创建md5对象 m = hashlib.md5(strs)
	m.update(strs)  # 生成机密串
	pwd = m.hexdigest()  # 获取加密串
	return pwd


#密码字符串池
pwdStrPool = '0123456789abcdefghijkmnpqrstuvwxyz~@#$%^&*()_+'\
    'ABCDEFGHIJKMNPQRSTUVWXYZ'
#密码字符串池长度
pwdStrPoolSize = len(pwdStrPool)
#定义要生成的密码长度
pwdLen = [16,16]

# 获取一个固定长度的随机串
def get_randNum(p):
	randStr = ''
	for i in range(p):
		randNum = random.randint(0, pwdStrPoolSize - 1)
		randStr += pwdStrPool[randNum]
	return randStr


# base64加密
def base64_encode(strs):
	str_encode = base64.encodestring(strs)
	return str_encode


# base64解密
def base64_decode(strs):
	str_decode = base64.decodestring(strs)
	return str_decode
